package com.rinlink.jbcodec.creator;

import com.alibaba.fastjson.JSONObject;
import com.rinlink.jbcodec.common.BytesUtils;
import com.rinlink.jbcodec.common.CreatorUtils;
import com.rinlink.jbcodec.exception.AttributePropertyException;
import com.rinlink.jbcodec.exception.FieldValueException;
import com.rinlink.jbcodec.transform.HexStringToBytes;
import com.rinlink.jbcodec.transform.Transform;

public class VariableHexStringToBytesCreator extends ObjectToBytesCreator<String> {

    @Override
    protected Transform<byte[]> create(String s, JSONObject attr, JSONObject source) {
        int bytes = CreatorUtils.getBytes(attr,source);

        if (bytes * 2 != s.length()){
            throw new FieldValueException();
        }
        return new HexStringToBytes(s);
    }
}
